package vip.zhenzicheng.nettybasic.sharehandler;

import io.netty.channel.ChannelDuplexHandler;
import io.netty.channel.ChannelHandler;
import io.netty.channel.ChannelHandlerContext;

import java.util.concurrent.atomic.AtomicLong;
import java.util.logging.Logger;

/**
 * 统计服务器接收和发送的业务总报文数
 *
 * @author zhenzicheng
 * @date 2022-06-08 19:20
 */
@ChannelHandler.Sharable
public class MessageCountHandler extends ChannelDuplexHandler {

  private static final Logger LOG = Logger.getLogger(MessageCountHandler.class.getSimpleName());

  private final AtomicLong inCount = new AtomicLong(0);
  private final AtomicLong outCount = new AtomicLong(0);

  @Override
  public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
    LOG.info("收到报文总数：" + inCount.incrementAndGet());
    super.channelRead(ctx, msg);
  }

  @Override
  public void flush(ChannelHandlerContext ctx) throws Exception {
    LOG.info("发出总报文数：" + outCount.incrementAndGet());
    super.flush(ctx);
  }
}
